package welsir.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import welsir.entity.dos.Setting;
import welsir.entity.dos.Square;
import welsir.entity.dos.SquareClass;
import welsir.entity.vo.SquareClassPageVO;
import welsir.mapper.SettingMapper;
import welsir.mapper.SquareClassMapper;
import welsir.mapper.SquareMapper;
import welsir.service.ISettingService;
import welsir.service.ISquareClassService;
import welsir.service.ISquareService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Genius
 * @date 2023/03/08 15:41
 **/
@Service
public class ISquareClassServiceImpl extends ServiceImpl<SquareClassMapper, SquareClass> implements ISquareClassService {

    @Resource
    private SquareClassMapper squareClassMapper;

    @Resource
    private SquareMapper squareMapper;

    @Override
    public SquareClassPageVO GetList(Integer page, Integer limit) {
        Page<SquareClass> SquareClassPage = new Page<>(page,limit);
        SquareClassPageVO classPageVO = new SquareClassPageVO();
        List<SquareClass> result = new ArrayList<>();
        QueryWrapper<SquareClass> wrapper= new QueryWrapper<>();
        wrapper.orderByDesc("id");

        Page<SquareClass> resultPage = this.squareClassMapper.selectPage(SquareClassPage, wrapper);
        List<SquareClass> squareClasses = resultPage.getRecords();
        for (SquareClass squareClass : squareClasses) {
            Integer id = squareClass.getId();
            QueryWrapper<Square> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("sort_class", id);
            Integer count = squareMapper.selectCount(queryWrapper);
            squareClass.setNum(count);
            result.add(squareClass);
        }
        long total = resultPage.getTotal();
        classPageVO.setData(result);
        classPageVO.setTotal(total);
        return classPageVO;
    }
}
